package com.zf.zffencheng.service.impl;

import com.zf.zffencheng.model.pojo.PalaceRegion;
import com.zf.zffencheng.model.pojo.TenantPalace;
import com.zf.zffencheng.model.vo.PalaceRegionVo;
import com.zf.zffencheng.service.PalaceRegionService;
import com.zf.zffencheng.service.TenantPalaceRegionService;
import com.zf.zffencheng.service.TenantPalaceService;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: PalaceRegionServiceImpl <br/>
 * Description:  <br/>
 * date: 2021/5/28 16:21<br/>
 *
 * @author william <br/>
 */
@Service
public class PalaceRegionServiceImpl implements PalaceRegionService {
    @Autowired
    private TenantPalaceService tenantPalaceService;
    @Autowired
    private TenantPalaceRegionService tenantPalaceRegionService;
    /**
     * 大殿-区域 树桩
     *
     * @param tenantId
     * @return
     */
    @Override
    public List<PalaceRegionVo> tree(Integer tenantId) {
        List<TenantPalace> tenantPalaces = tenantPalaceService.selectByTenantIdDesc(tenantId);
        List<PalaceRegionVo> collect = tenantPalaces.stream().map(val -> {
            PalaceRegionVo vo = new PalaceRegionVo();
            vo.setId(val.getId());
            vo.setName(val.getPalaceName());
            return vo;
        }).collect(Collectors.toList());
        for (PalaceRegionVo vo : collect) {
            List<PalaceRegion> palaceRegions =  tenantPalaceRegionService.selectByPalaceIdDesc(vo.getId());
            List<PalaceRegionVo> children = palaceRegions.stream().map(palaceRegion -> {
                PalaceRegionVo child = new PalaceRegionVo();
                child.setId(palaceRegion.getId());
                child.setName(palaceRegion.getRegionName());
                return child;
            }).collect(Collectors.toList());
            vo.setChildren(children.isEmpty()? null :children);
        }
        return collect;
    }
}
